﻿using googleMap.Web.Dao;
using googleMap.Web.Rsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace googleMap.Web.Controllers
{
    public class MapServiceController : Controller
    {
        private bool IsInnerIPByFunction()
        {
            string ip = Request.UserHostAddress;

            var args = ip.Split('.');

            if (args.Length > 2 && args[0] == "192" && args[1] == "168")
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 內外網IP地址
        /// </summary>
        /// <returns></returns>
        public ActionResult IsInnerIP()
        {
            return Json(new { status = this.IsInnerIPByFunction(), message = "" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 獲取水位站行政區
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWasRiverBasinList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {

                    var list = mapsDao.gm2012_ctl_was_river_basin.OrderBy(m => m.StreamAscOrderNO).ToList();

                    var waslist = mapsDao.maps_gm2012_ctl_was_station.ToList();

                    var outlist = new List<gm2012_ctl_was_river_basin>();
                    foreach (var item in list)
                    {
                        if (waslist.Where(m => m.RiverBasinNO == item.RiverBasinNO).Count() > 0)
                        {
                            outlist.Add(item);
                        }
                    }
                    return Json(new { status = true, message = "", data = outlist }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message}, JsonRequestBehavior.AllowGet);
            }
            
        }

        /// <summary>
        /// 獲取下水道行政區
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSewerDistrictList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.dim_district.OrderBy(m => m.AscOrderNO).ToList();
                    return Json(new { status = true, message = "", data = list }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 得到影像站分區
        /// </summary>
        /// <returns></returns>
        public ActionResult GetImageDistrict()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.dim_district.OrderBy(m => m.AscOrderNO).ToList();

                    var imageList = mapsDao.dim_live_image_station.ToList();

                    var outlist = new List<dim_district>();
                    foreach (var item in list)
                    {
                        if (imageList.Where(m => m.DistrictNO == item.DistrictNO).Count() > 0)
                        {
                            outlist.Add(item);
                        }
                    }
                    return Json(new { status = true, message = "", data = outlist }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        /// <summary>
        /// 得到影像站流域
        /// </summary>
        /// <returns></returns>
        public ActionResult GetImageRiverBasin()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.gm2012_ctl_image_river_basin.OrderBy(m => m.StreamAscOrderNO).ToList();

                    var imageList = mapsDao.dim_live_image_station.ToList();

                    var outlist = new List<gm2012_ctl_image_river_basin>();
                    foreach (var item in list)
                    {
                        if (imageList.Where(m => m.RiverBasinNO == item.RiverBasinNO).Count() > 0)
                        {
                            outlist.Add(item);
                        }
                    }
                    return Json(new { status = true, message = "", data = outlist }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 获取影像站列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetImageGroupByList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var IsInnerURL = this.IsInnerIPByFunction();
                    
                    var list = mapsDao.maps_dim_live_image_station.OrderBy(m => m.AscOrderNO)
                        .ToList();
                    var ImageGroupList = new List<Rsp.Rsp_maps_dim_live_image_station>();
                    foreach (var item in list
                                            .Where(m => m.ParentStationNO == null)
                                            .OrderBy(m => m.AscOrderNO)
                                            .ToList())
                    {
                        var obj = new Rsp.Rsp_maps_dim_live_image_station();

                        obj.AscOrderNO = item.AscOrderNO;
                        obj.DistrictAscOrderNO = item.DistrictAscOrderNO;
                        obj.RiverBasinName = item.RiverBasinName;
                        obj.RiverBasinNO = item.RiverBasinNO;
                        obj.DistrictName = item.DistrictName;
                        obj.DistrictNO = item.DistrictNO;
                        obj.IsSub = item.IsSub;
                        obj.Lat = item.Lat;
                        obj.Layer = item.Layer;
                        obj.Lng = item.Lng;
                        obj.ParentStationNO = item.ParentStationNO;
                        obj.StationName = item.StationName;
                        obj.StationNO = item.StationNO;
                        obj.StationType = item.StationType;
                        obj.URL = IsInnerURL == true ? item.InnerURL : item.URL;
                        obj.URL += "/snapshot";
                        obj.SubCount = 1;
                        if (item.IsSub == 1)
                        {
                            obj.SubCount = list.Where(m => m.ParentStationNO == item.StationNO).Count();
                        }
                        ImageGroupList.Add(obj);
                    }
                    return Json(new { status = true, message = "", data = ImageGroupList }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult GetImageByList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var IsInnerURL = this.IsInnerIPByFunction();
                    var list = mapsDao.maps_dim_live_image_station
                    .Where(m => m.URL != null)
                    .OrderBy(m => m.RiverBasinAsc).ThenBy(m => m.DistrictAscOrderNO).ThenBy(m => m.AscOrderNO)
                    .ToList();

                    foreach (var item in list)
                    {
                        if (IsInnerURL)
                        {
                            item.URL = item.InnerURL + "/snapshot";
                        }
                        else
                        {
                            item.URL = item.URL + "/snapshot";
                        }
                    }
                    return Json(new { status = true, message = "", data = list }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 用戶登入
        /// </summary>
        /// <param name="UserID">帳號</param>
        /// <param name="Password">密碼</param>
        /// <returns></returns>

        public ActionResult UserLogin(string UserID, string Password)
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    string msg = "";

                    var entity = mapsDao.dim_app_user.Where(PP => PP.UserID == UserID).FirstOrDefault();
                    if (msg == "")
                    {
                        if (entity == null)
                        {
                            msg = "帳號或密碼錯誤，請重新輸入。";
                            throw new Exception(msg);
                        }
                    }
                    if (msg == "")
                    {
                        if (entity.IsValid == 0)
                        {
                            msg = "帳號已停用。";
                            throw new Exception(msg);
                        }
                    }
                    if (msg == "")
                    {
                        if (entity.Password != Password)
                        {
                            msg = "帳號或密碼錯誤，請重新輸入。";
                            throw new Exception(msg);
                        }
                    }

                    int app_UserID = entity == null ? 0 : entity.app_UserID;

                    var jsonResult = new
                    {
                        UserID = app_UserID.ToString(),
                        ErrorMsg = msg,
                        photourl = "http://khm0.google.com.tw/kh/v=148&src=app&x={1}&y={2}&z={3}&s=Galile"
                    };
                    return Json(new { status = true, message = "", data = jsonResult }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetRanByList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.maps_gm2012_ctl_ran_station.Where(PP => PP.IsDisplay == 1)
                        .OrderBy(m => m.AscOrderNO).ThenBy(m => m.DistrictAsc).ToList();
                    return Json(new { status = true, message = "", data = list }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetRanZoneList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.gm2012_ctl_ran_zone.ToList();
                    return Json(new { status = true, message = "", data = list }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetRanByID(string StationNO)
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var obj = mapsDao.maps_gm2012_ctl_ran_station.Where(pp=>pp.StationNO == StationNO);
                    return Json(new { status = true, message = "", data = obj }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }

            
        }

        public ActionResult GetWasByList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.maps_gm2012_ctl_was_station.Where(PP => PP.IsDisplay == 1).OrderBy(m => m.AscOrderNO).ThenBy(m => m.riverAsc).ToList();
                    return Json(new { status = true, message = "", data = list }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetWasByID(string StationNO)
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var obj = mapsDao.maps_gm2012_ctl_was_station.Where(PP => PP.StationNO == StationNO).First();
                    return Json(new { status = true, message = "", data = obj }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetSewerByList()
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var list = mapsDao.maps_gm2012_ctl_sewer_station.Where(PP => PP.IsDisplay == 1).OrderBy(m => m.AscOrderNO).ThenBy(m => m.riverasc).ToList();
                    return Json(new { status = true, message = "", data = list }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetSewerByID(string StationNO)
        {
            try
            {
                using (var mapsDao = new pwbGoogleMapsEntities())
                {
                    var obj = mapsDao.maps_gm2012_ctl_sewer_station.Where(PP => PP.StationNO == StationNO).First();
                    return Json(new { status = true, message = "", data = obj }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}